﻿using BackData;
using Domain;
using Infratructure;
using MediatR;
using Rabbit.API.Application.Command.Breed;

namespace Rabbit.API.Application.Handler.Breed
{
    public class BreedGetHandler:IRequestHandler<BreedGetCommand,APIResult<BreedModel>>
    {
        private readonly IRepository<BreedModel> breedway;
        private readonly ILogger<BreedGetHandler> log;

        public BreedGetHandler(IRepository<BreedModel> breedway,ILogger<BreedGetHandler> log)
        {
            this.breedway = breedway;
            this.log = log;
        }
        /// <summary>
        /// 获取商品养殖信息
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>商品养殖数据</returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<BreedModel>> Handle(BreedGetCommand request, CancellationToken cancellationToken)
        {
            APIResult<BreedModel> res = new APIResult<BreedModel>();
            try
            {
                var breed = breedway.GetAll().FirstOrDefault(x => x.Id == request.BreedId && x.IsDel == false);
                if(breed!=null)
                {
                    log.LogInformation($"成功获取商品养殖信息");
                    res.Message = $"成功获取商品养殖信息";
                    res.Num = StateCode.OK;
                    res.Data = breed;
                }
                else
                {
                    res.Message = $"获取商品养殖信息失败";
                    res.Num = StateCode.Fail;
                }
                return Task.FromResult(res);
            }
            catch (Exception ex)
            {
                log.LogError($"获取商品养殖信息发生异常{ex.Message}");
                throw;
            }
        }
    }
}
